<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Form.Classes.Model
 * @package   PsiFramework.Plugins.Form.Classes.Model
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Model\FieldType.html
 */

namespace PsiFramework\Plugins\Form\Classes\Model;

import("Classes.Model.Object");
import("Plugins.Form.Classes.Util.Json.JsonForm");
import("Plugins.Form.Classes.Model.FormField");
import("Classes.Util.Parameters.ObjectParameters");

use \PsiFramework\Classes\Model\Object;
use \PsiFramework\Plugins\Form\Classes\Util\Json\JsonForm;
use \PsiFramework\Plugins\Form\Classes\Model\FormField;
use \PsiFramework\Classes\Util\Parameters\ObjectParameters;

/**
 * The FieldType Object
 *
 * This is the class for the FieldType Object
 *
 * @category PsiFramework.Plugins.Form.Classes.Model
 * @package  PsiFramework.Plugins.Form.Classes.Model
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Model\FieldType.html
 */
class FieldType extends Object
{

    /**
     * @var int
     */
    protected /*int*/              $_id ;
    /**
     * @var string
     */
    protected /*string*/           $_name ;
    /**
     * 
     * @var JsonForm
     */
    protected /*JsonForm*/         $_structure ;
    /**
     * @var ObjectParameters
     */
    protected /*ObjectParameters*/ $_responseParameters ;
    /**
     * @var array
     */
    protected /*FormField[]*/      $_fields ;

    /**
     * Constructor
     * 
     * @param int              $id                 The id for the FieldType
     * @param string           $name               The name of the FieldType
     * @param JsonForm         $structure          The structure of
     *                                             the FieldType response
     * @param ObjectParameters $responseParameters The ObjectParameters for
     *                                             the FieldType response
     * @param FormField[]      $fields             The fiels of the FieldType
     * 
     * @return FieldType
     */
    public function __construct(
        /*int*/          $id=null,
        /*string*/       $name=null,
        JsonForm         $structure=null,
        ObjectParameters $responseParameters=null,
        /*FormField[]*/  $fields=null
    )
    {
        $this->_id = $id ;
        $this->_name = $name ;
        $this->_structure = $structure ;
        $this->_responseParameters = $responseParameters ;
        $this->_fields = $fields ;
    }

    /**
     * Getter for the id / primary key
     * 
     * @see Object::getId()
     * 
     * @return int
     */
    public function getId()
    {
        return $this->_id ;
    }

    /**
     * Setter for the id 
     * 
     * @param int $id The id
     * 
     * @return void
     */
    public function setId(/*int*/ $id)
    {
        $this->_id = $id ;
    }

    /**
     * Getter for the name
     * 
     * @return string
     */
    public function getName()
    {
        return $this->_name ;
    }

    /**
     * Setter for the name
     *
     * @param string $name The name
     *
     * @return void
     */
    public function setName(/*string*/ $name)
    {
        $this->_name = $name ;
    }

    /**
     * The function that returns the fields descriptors
     * 
     * @return array
     */
    public function getFieldsDescriptors()
    {
        $array = array();
        $jsonForm = $this->_structure ;

        foreach ($jsonForm->getElements() as $field) {
            foreach ($field->getElements() as $element) {
                $label = $element->getLabel();
                if (startsWith($label, '<') && endsWith($label, '>')) {
                    $label = substr($label, 1, strlen($label)-2);
                    $array[] = $label ;
                }
            }
        }

        return $array ;
    }

    /**
     * Getter for the structure
     *
     * @return JsonForm
     */
    public function getStructure()
    {
        return $this->_structure ;
    }

    /**
     * Setter for the structure
     *
     * @param JsonForm $structure The structure
     *
     * @return void
     */
    public function setStructure(JsonForm $structure)
    {
        $this->_structure = $structure ;
    }

    /**
     * Getter for the response parameters
     *
     * @return ObjectParameters
     */
    public function getResponseParameters()
    {
        return $this->_responseParameters ;
    }

    /**
     * Setter for the response parameters
     *
     * @param ObjectParameters $responseParameters The response parameters
     *
     * @return void
     */
    public function setResponseParameters(
        ObjectParameters $responseParameters
    )
    {
        $this->_responseParameters = $responseParameters ;
    }

    /**
     * Getter for the fields
     *
     * @return array
     */
    public function getFields()
    {
        return $this->_fields ;
    }

    /**
     * Setter for the fields
     *
     * @param array $fields The fields
     *
     * @return void
     */
    public function setFields(/*FormField[]*/ $fields)
    {
        $this->_fields = $fields ;
    }

    /**
     * Shows the FieldType as a string
     * 
     * @see Object::toString()
     * 
     * @return string
     */
    public function toString()
    {
        return $this->_name ;
    }

}
